/**
 * jims
 */
package com.jims.master.modules.blood.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.blood.entity.BloodComponent;
import com.jims.master.modules.blood.service.BloodComponentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * 血液成分字典Controller
 * @author fengyg
 * @version 2017-04-06
 */
@Controller
@RequestMapping(value = "${adminPath}/blood/bloodComponent")
public class BloodComponentController extends BaseController {

	@Autowired
	private BloodComponentService bloodComponentService;
	
	@ModelAttribute
	public BloodComponent get(@RequestParam(required=false) String id) {
		BloodComponent entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = bloodComponentService.get(id);
		}
		if (entity == null){
			entity = new BloodComponent();
		}
		return entity;
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(BloodComponent bloodComponent, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<BloodComponent> page = bloodComponentService.findPage(new Page<BloodComponent>(request, response), bloodComponent); 
		model.addAttribute("page", page);
		return "modules/blood/bloodComponentIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(BloodComponent bloodComponent, Model model) {
		model.addAttribute("bloodComponent", bloodComponent);
		return "modules/blood/bloodComponentForm";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(BloodComponent bloodComponent, Model model) {
	    StringData data=new StringData();
		int i=bloodComponentService.save(bloodComponent);
        if(i>0){
            data.setCode("success");
            data.setData("保存血液成分字典维护成功");
        }else{
            data.setCode("error");
            data.setData("保存血液成分字典维护失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(BloodComponent bloodComponent) {
	    StringData data=new StringData();
		int i=bloodComponentService.delete(bloodComponent);
        if(i>0){
            data.setCode("success");
            data.setData("删除血液成分字典维护成功");
        }else{
            data.setCode("error");
            data.setData("删除血液成分字典维护失败");
        }
		return data;
	}

	/**
	 * @param q 传递参数
	 * @return List    返回类型
	* @throws
	 * @Title: autoComplete
	 * @Desciption: (用血申请处，血液要求自动补全)
	 * @author CTQ
	 * @date 2017-04-10
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"autoComplete", ""})
	@ResponseBody
	public List<BloodComponent> autoComplete(String q, String limit, String type) {
		if(q!=null&&!q.equals("")){
			try {
				q = URLDecoder.decode(q,"UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		return bloodComponentService.findListByParam(q,limit);
	}

}